/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.venta;

import Controller.ModeloC;
import Controller.TiendaC;
import Controller.UsuarioC;
import Model.ModeloM;
import Model.TiendaM;
import Model.UsuarioM;
import Model.VentaDetalleM;
import Model.VentaM;
import Util.ArrayListTableModel;
import Util.AutoJComboBox;
import Util.MiNroVenta;
import Util.MiReloj;
import Util.MiRenderBoton;
import View.login.frmInicioSesion;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Maryluz
 */
public class frmVenta extends javax.swing.JFrame {

    public static int xIdTienda, xOtraTienda;
    VentaM oVentaM;
    String[] listaUsuario;
    SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", new Locale("ES"));
    Date fechaDate = new Date();
    String fecha = formateador.format(fechaDate);
    public static ArrayListTableModel oModelTblas;
    int anterior = 0;
    ImageIcon icon;
    TiendaM oTienda;
    
    @SuppressWarnings("OverridableMethodCallInConstructor")
    public frmVenta(int idTienda, TiendaM oM) {
        cargarComboVendedores();
        initComponents();
        txtFecha.setText(fecha);
        icon = new ImageIcon(getClass().getResource("/Image/SysRealeza2.png"));
        this.setIconImage(icon.getImage()); 
        inicializarCampos(oM);
        xIdTienda = idTienda;
        xOtraTienda = otraTienda(xIdTienda);
        cargarModelo(xIdTienda);
        oTienda= oM;
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
        tbnModelo.requestFocus();
        tbnModelo.setRowHeight(45);
        tbnTiendas.setRowHeight(28);
        tbnVenta.setRowHeight(45);
        configurarTabla();
        oModelTblas = (ArrayListTableModel) tbnVenta.getModel();
        txtDesc.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();
                String texto = txtDesc.getText();

                if (texto.length() < 2) {
                    // Verificar si la tecla pulsada no es un digito
                    if (((caracter < '0') || (caracter > '9')) && (caracter != '\b' /*corresponde a BACK_SPACE*/)) {
                        e.consume();  // ignorar el evento de teclado
                    }
                } else {
                    e.consume();
                }
            }
        });
        configurar();
        MiNroVenta oMiReloj=new MiNroVenta(txtNroVenta);
        oMiReloj.start();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        btnRegresar = new javax.swing.JButton();
        btnCobrar = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbnVenta = new javax.swing.JTable();
        txtSubTotal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtDescuento = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtIgv = new javax.swing.JTextField();
        txtTotal = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtDesc = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbnModelo = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbnTiendas = new javax.swing.JTable();
        btnAlmacen = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        txtUsuario = new javax.swing.JTextField();
        txtTienda = new javax.swing.JTextField();
        jPanel10 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        txtCliente = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        cbnVendedor = new AutoJComboBox(listaUsuario);
        jLabel6 = new javax.swing.JLabel();
        txtNroVenta = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtFecha = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        cbnDocumento = new javax.swing.JComboBox();
        jPanel11 = new javax.swing.JPanel();
        banner = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        pie_Pagina = new javax.swing.JLabel();

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/buy.png"))); // NOI18N
        jButton4.setText("<HTML> NUEVA <BR> VENTA</HTML>");
        jButton4.setEnabled(false);
        jButton4.setPreferredSize(new java.awt.Dimension(100, 51));

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/preview.png"))); // NOI18N
        jButton3.setText("<HTML> CONSULTAR<BR> VENTAS</HTML>");
        jButton3.setEnabled(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SysRealeza - Módulo de Ventas");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/regresar.png"))); // NOI18N
        btnRegresar.setToolTipText("Regresar al Menú Principal");
        btnRegresar.setFocusable(false);
        btnRegresar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRegresar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegresarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnRegresar);

        btnCobrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCobrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/newspaper.png"))); // NOI18N
        btnCobrar.setText("<HTML> COBRAR <BR> VENTA</HTML>");
        btnCobrar.setActionCommand("");
        btnCobrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCobrarActionPerformed(evt);
            }
        });

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tbnVenta.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        tbnVenta.setModel(new ArrayListTableModel(
            new Object [][] {},
            new String [] {" CÓDIGO"," DESCRIPCIÓN"," CANTIDAD"," PRECIO", " IMPORTE"," ", ""}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class,java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,true,false,false,false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbnVenta.getTableHeader().setReorderingAllowed(false);
        tbnVenta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbnVentaMouseClicked(evt);
            }
        });
        tbnVenta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbnVentaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tbnVenta);

        txtSubTotal.setEditable(false);
        txtSubTotal.setBackground(new java.awt.Color(255, 255, 255));
        txtSubTotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("SUB TOTAL :");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("DESCUENTO (%) :");

        txtDescuento.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDescuento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDescuentoFocusLost(evt);
            }
        });
        txtDescuento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDescuentoKeyReleased(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("TOTAL :");

        txtIgv.setEditable(false);
        txtIgv.setBackground(new java.awt.Color(255, 255, 255));
        txtIgv.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        txtTotal.setEditable(false);
        txtTotal.setBackground(new java.awt.Color(255, 255, 255));
        txtTotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("IGV :");

        txtDesc.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDesc.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDescFocusLost(evt);
            }
        });
        txtDesc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDescKeyReleased(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 126, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addGap(20, 20, 20)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel8Layout.createSequentialGroup()
                            .addComponent(jLabel8)
                            .addGap(18, 18, 18)
                            .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel8Layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                                    .addComponent(txtDesc, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtIgv, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSubTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(8, 8, 8)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(txtDesc, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtIgv, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(8, 8, 8)
                        .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))))
                .addGap(36, 36, 36))
        );

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tbnModelo.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N
        tbnModelo.setModel(new ArrayListTableModel(
            new Object [][] {},
            new String [] {" DESCRIPCIÓN"," STOCK"," PRECIO",""}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbnModelo.getTableHeader().setReorderingAllowed(false);
        tbnModelo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbnModeloMouseClicked(evt);
            }
        });
        tbnModelo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbnModeloKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tbnModelo);

        tbnTiendas.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tbnTiendas.setModel(new ArrayListTableModel(
            new Object [][] {},
            new String [] {" TIENDA"," STOCK",""}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbnTiendas.getTableHeader().setReorderingAllowed(false);
        tbnTiendas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbnTiendasMouseClicked(evt);
            }
        });
        tbnTiendas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbnTiendasKeyReleased(evt);
            }
        });
        jScrollPane3.setViewportView(tbnTiendas);

        btnAlmacen.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAlmacen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/PoloS.png"))); // NOI18N
        btnAlmacen.setText("<HTML>MOVIMIENTOS<BR>DE ALMACEN</HTML>");
        btnAlmacen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlmacenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(btnAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAlmacen, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setPreferredSize(new java.awt.Dimension(335, 80));

        txtUsuario.setEditable(false);
        txtUsuario.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N
        txtUsuario.setForeground(new java.awt.Color(0, 0, 102));
        txtUsuario.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtUsuario.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        txtUsuario.setPreferredSize(new java.awt.Dimension(6, 22));
        txtUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUsuarioActionPerformed(evt);
            }
        });

        txtTienda.setEditable(false);
        txtTienda.setFont(new java.awt.Font("Calibri", 1, 18)); // NOI18N
        txtTienda.setForeground(new java.awt.Color(51, 0, 0));
        txtTienda.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTienda.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        txtTienda.setPreferredSize(new java.awt.Dimension(6, 22));
        txtTienda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTiendaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTienda, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtTienda, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel10.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(102, 0, 0));
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel10.setText("CLIENTE ");

        txtCliente.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N

        jLabel11.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(102, 0, 0));
        jLabel11.setText("VENDEDOR(A) ");

        cbnVendedor.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(102, 0, 0));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("Nro VENTA");
        jLabel6.setPreferredSize(new java.awt.Dimension(80, 14));

        txtNroVenta.setEditable(false);
        txtNroVenta.setFont(new java.awt.Font("Arial", 1, 16)); // NOI18N
        txtNroVenta.setForeground(new java.awt.Color(0, 0, 51));
        txtNroVenta.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel5.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(102, 0, 0));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel5.setText("FECHA ");
        jLabel5.setPreferredSize(new java.awt.Dimension(80, 14));

        txtFecha.setEditable(false);
        txtFecha.setFont(new java.awt.Font("Arial", 1, 16)); // NOI18N
        txtFecha.setForeground(new java.awt.Color(0, 0, 51));
        txtFecha.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFecha.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel12.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(102, 0, 0));
        jLabel12.setText("TIPO DE DOCUMENTO :");

        cbnDocumento.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        cbnDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Boleta", "Factura" }));
        cbnDocumento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbnDocumentoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel10Layout.createSequentialGroup()
                        .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel12)
                        .addGap(18, 18, 18)
                        .addComponent(cbnDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel11)
                        .addGap(18, 18, 18)
                        .addComponent(cbnVendedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel10Layout.createSequentialGroup()
                        .addComponent(txtNroVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNroVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(cbnDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(cbnVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel11.setBackground(new java.awt.Color(0, 0, 0));
        jPanel11.setForeground(new java.awt.Color(0, 0, 51));

        banner.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        banner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/2.png"))); // NOI18N

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addComponent(banner)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addComponent(banner)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jButton6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/maintenance_time.png"))); // NOI18N
        jButton6.setText("<HTML> VENTAS<BR> DEL DÍA</HTML>");
        jButton6.setEnabled(false);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        pie_Pagina.setBackground(new java.awt.Color(0, 0, 0));
        pie_Pagina.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/pie de pagina.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(btnCobrar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pie_Pagina, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnCobrar, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pie_Pagina)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegresarActionPerformed
        frmInicioSesion.menu.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnRegresarActionPerformed

    private void txtTiendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTiendaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTiendaActionPerformed

    @SuppressWarnings("static-access")
    private void tbnModeloKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbnModeloKeyReleased
        if (evt.getKeyCode() == evt.VK_SPACE) {
            new frmCantidad(this, true, (ModeloM) tbnModelo.getValueAt(tbnModelo.getSelectedRow(), 3)).setVisible(true);
        }

        if (evt.getKeyCode() == evt.VK_UP || evt.getKeyCode() == evt.VK_DOWN) {
            int fila = tbnModelo.getSelectedRow();
            ModeloM oM = (ModeloM) tbnModelo.getValueAt(fila, 3);
            cargarTienda(xOtraTienda, oM.getIdmodelo());
        }

    }//GEN-LAST:event_tbnModeloKeyReleased

    private void tbnModeloMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbnModeloMouseClicked
        if (evt.getClickCount() == 2) {
            int FilaSeleccionada = tbnModelo.getSelectedRow();
            if (FilaSeleccionada != -1) {
                //System.out.print(((ModeloM) tbnModelo.getValueAt(FilaSeleccionada, 3)).getPrecioMayor());
                new frmCantidad(this, true, (ModeloM) tbnModelo.getValueAt(FilaSeleccionada, 3)).setVisible(true);
            }
        }
        if (evt.getClickCount() == 1) {
            int fila = tbnModelo.getSelectedRow();
            ModeloM oM = (ModeloM) tbnModelo.getValueAt(fila, 3);
            cargarTienda(xOtraTienda, oM.getIdmodelo());
        }
    }//GEN-LAST:event_tbnModeloMouseClicked

    private void tbnTiendasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbnTiendasMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tbnTiendasMouseClicked

    private void tbnTiendasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbnTiendasKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_tbnTiendasKeyReleased

    private void btnCobrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCobrarActionPerformed
        if (tbnVenta.getRowCount() != 0) {
            oVentaM = new VentaM();
            oVentaM.setFecha(txtFecha.getText());
            oVentaM.setIdTienda(xIdTienda);
            oVentaM.setTienda(txtTienda.getText());
            oVentaM.setIdVendedor(new UsuarioC().getIdVendedor(cbnVendedor.getSelectedItem().toString()));
            oVentaM.setVendedor(cbnVendedor.getSelectedItem().toString());
            oVentaM.setTipoDocumento(cbnDocumento.getSelectedIndex() + 1);
            oVentaM.setCliente(txtCliente.getText());
            oVentaM.setNumVenta(txtNroVenta.getText());
            oVentaM.setSubTotal(Double.parseDouble(txtSubTotal.getText()));
            oVentaM.setTotal(Double.parseDouble(txtTotal.getText()));
            try {
                oVentaM.setDescuento(Double.parseDouble(txtDescuento.getText()));
            } catch (Exception e) {
                oVentaM.setDescuento(0.0);
            }

            try {
                oVentaM.setIgv(Double.parseDouble(txtIgv.getText()));
            } catch (Exception e) {
                oVentaM.setIgv(0.0);
            }
            VentaDetalleM[] oDetalleMs;
            oDetalleMs = new VentaDetalleM[tbnVenta.getRowCount()];
            for (int i = 0; i < tbnVenta.getRowCount(); i++) {
                oDetalleMs[i] = new VentaDetalleM();
                oDetalleMs[i].setIdPrenda(Integer.parseInt(tbnVenta.getValueAt(i, 0).toString().trim()));
                oDetalleMs[i].setModelo(tbnVenta.getValueAt(i, 1).toString());
                oDetalleMs[i].setIdTienda(oVentaM.getIdTienda());
                oDetalleMs[i].setCantidad(Integer.parseInt(tbnVenta.getValueAt(i, 2).toString()));
                oDetalleMs[i].setImporte(Double.parseDouble(tbnVenta.getValueAt(i, 4).toString()));
            }

            new frmEfectivoVuelto(this, true, oVentaM, oDetalleMs).setVisible(true);
//            calcularNroVenta oNroVenta = new calcularNroVenta(frmVenta.txtFecha.getText());
//            frmVenta.txtFecha.setText(oNroVenta.convertirNroventa());  
        } else {
            JOptionPane.showMessageDialog(null, "Ingrese alguna prenda a vender.");
        }
    }//GEN-LAST:event_btnCobrarActionPerformed

    @SuppressWarnings({"static-access", "UnusedAssignment"})
    private void tbnVentaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbnVentaKeyReleased

        if (evt.getKeyCode() == evt.VK_ENTER) {
            ModeloM modeloM = (ModeloM) tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 6);
            int cantidad = 0;
            double canti = 0;
            if (!"".equals(tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 2).toString())) {
                try {
                    cantidad = Integer.parseInt(tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 2).toString());
                    System.out.println("cantidad : " + anterior);
                } catch (Exception e) {
                    //cantidad = (int) (Double.parseDouble(tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 4).toString()) / Double.parseDouble(tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 3).toString()));
                    cantidad = anterior;
                    if (cantidad >= 3) {
                        //System.out.println("m"+modeloM.getPrecioMayor());
                        canti = modeloM.getPrecioMayor();
                    } else {
                        // System.out.println("me"+modeloM.getPrecio());
                        canti = modeloM.getPrecio();
                    }
                    frmVenta.tbnVenta.setValueAt(cantidad, tbnVenta.getSelectedRow(), 2);
                    frmVenta.tbnVenta.setValueAt(canti, tbnVenta.getSelectedRow(), 3);
                }

            } else {
                frmVenta.tbnVenta.setValueAt(0, tbnVenta.getSelectedRow(), 2);
                // frmVenta.tbnVenta.setValueAt(Integer.parseInt(tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 2).toString()) * Double.parseDouble(frmVenta.tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 3).toString()), tbnVenta.getSelectedRow(), 4);
            }

            if (cantidad <= modeloM.getStockModelo()) {
                if (cantidad >= 3) {
                    System.out.println("m" + modeloM.getPrecioMayor());
                    canti = modeloM.getPrecioMayor();
                } else {
                    System.out.println("me" + modeloM.getPrecio());
                    canti = modeloM.getPrecio();
                }
                frmVenta.tbnVenta.setValueAt(cantidad, tbnVenta.getSelectedRow(), 2);
                frmVenta.tbnVenta.setValueAt(canti, tbnVenta.getSelectedRow(), 3);
                frmVenta.tbnVenta.setValueAt((cantidad) * canti, tbnVenta.getSelectedRow(), 4);
            } else {
                double cant = Double.parseDouble(tbnVenta.getValueAt(tbnVenta.getSelectedRow(), 4).toString()) / canti;
                //frmVenta.tbnVenta.setValueAt((int) cant, tbnVenta.getSelectedRow(), 2);
                frmVenta.tbnVenta.setValueAt(anterior, tbnVenta.getSelectedRow(), 2);
            }

            calcularImporte();
        }

    }//GEN-LAST:event_tbnVentaKeyReleased

    private void txtDescFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDescFocusLost
        calcularImporte();
    }//GEN-LAST:event_txtDescFocusLost

    @SuppressWarnings("static-access")
    private void txtDescKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER && !"".equals(txtDesc.getText().trim())) {
            calcularImporte();
        } else {
            txtDescuento.setText("");
        }
    }//GEN-LAST:event_txtDescKeyReleased

    private void cbnDocumentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbnDocumentoActionPerformed
        calcularImporte();
    }//GEN-LAST:event_cbnDocumentoActionPerformed

    private void tbnVentaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbnVentaMouseClicked
        if (tbnVenta.getSelectedColumn() == 5) {
            if (tbnVenta.getRowCount() != 0) {
                ArrayListTableModel dtm = (ArrayListTableModel) tbnVenta.getModel(); //TableProducto es el nombre de mi tabla ;)
                dtm.removeRow(tbnVenta.getSelectedRow());

                calcularImporte();
            }
        }
    }//GEN-LAST:event_tbnVentaMouseClicked

    @SuppressWarnings("static-access")
    private void txtDescuentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescuentoKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER) {
            calcularImporte();
        }
    }//GEN-LAST:event_txtDescuentoKeyReleased

    private void txtDescuentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDescuentoFocusLost
        calcularImporte();
    }//GEN-LAST:event_txtDescuentoFocusLost

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_F10) {
            //System.out.println(evt.getKeyCode());
        }
    }//GEN-LAST:event_formKeyPressed

    private void btnAlmacenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlmacenActionPerformed
        new frmMovimientoAlmacen(this, true, txtFecha.getText(), cbnVendedor.getSelectedItem().toString(),oTienda).setVisible(true);
        cargarModelo(xIdTienda);
    }//GEN-LAST:event_btnAlmacenActionPerformed

    private void txtUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUsuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtUsuarioActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton6ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        frmInicioSesion.menu.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel banner;
    private javax.swing.JButton btnAlmacen;
    public static javax.swing.JButton btnCobrar;
    private javax.swing.JButton btnRegresar;
    public static javax.swing.JComboBox cbnDocumento;
    private javax.swing.JComboBox cbnVendedor;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel pie_Pagina;
    public static javax.swing.JTable tbnModelo;
    private javax.swing.JTable tbnTiendas;
    public static javax.swing.JTable tbnVenta;
    public static javax.swing.JTextField txtCliente;
    public static javax.swing.JTextField txtDesc;
    public static javax.swing.JTextField txtDescuento;
    public static javax.swing.JTextField txtFecha;
    public static javax.swing.JTextField txtIgv;
    public static javax.swing.JTextField txtNroVenta;
    public static javax.swing.JTextField txtSubTotal;
    private javax.swing.JTextField txtTienda;
    public static javax.swing.JTextField txtTotal;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables

    private void configurarTabla() {
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);

        tbnModelo.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tbnModelo.getColumnModel().getColumn(2).setCellRenderer(tcr);

        tbnModelo.getColumnModel().getColumn(1).setMinWidth(70);
        tbnModelo.getColumnModel().getColumn(1).setPreferredWidth(70);
        tbnModelo.getColumnModel().getColumn(1).setMaxWidth(70);
        tbnModelo.getColumnModel().getColumn(2).setMinWidth(70);
        tbnModelo.getColumnModel().getColumn(2).setPreferredWidth(70);
        tbnModelo.getColumnModel().getColumn(2).setMaxWidth(70);
        tbnModelo.getColumnModel().getColumn(3).setMinWidth(0);
        tbnModelo.getColumnModel().getColumn(3).setPreferredWidth(0);
        tbnModelo.getColumnModel().getColumn(3).setMaxWidth(0);

        tbnTiendas.getColumnModel().getColumn(1).setCellRenderer(tcr);

        tbnTiendas.getColumnModel().getColumn(1).setMinWidth(50);
        tbnTiendas.getColumnModel().getColumn(1).setPreferredWidth(50);
        tbnTiendas.getColumnModel().getColumn(1).setMaxWidth(50);
        tbnTiendas.getColumnModel().getColumn(2).setMinWidth(0);
        tbnTiendas.getColumnModel().getColumn(2).setPreferredWidth(0);
        tbnTiendas.getColumnModel().getColumn(2).setMaxWidth(0);

        //tbnVenta.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tbnVenta.getColumnModel().getColumn(2).setCellRenderer(tcr);
        tbnVenta.getColumnModel().getColumn(3).setCellRenderer(tcr);
        tbnVenta.getColumnModel().getColumn(4).setCellRenderer(tcr);

        tbnVenta.getColumnModel().getColumn(1).setMinWidth(250);
        tbnVenta.getColumnModel().getColumn(1).setPreferredWidth(250);
        tbnVenta.getColumnModel().getColumn(1).setMaxWidth(250);
        tbnVenta.getColumnModel().getColumn(5).setMinWidth(50);
        tbnVenta.getColumnModel().getColumn(5).setPreferredWidth(50);
        tbnVenta.getColumnModel().getColumn(5).setMaxWidth(50);
        tbnVenta.getColumnModel().getColumn(6).setMinWidth(0);
        tbnVenta.getColumnModel().getColumn(6).setPreferredWidth(0);
        tbnVenta.getColumnModel().getColumn(6).setMaxWidth(0);
        tbnVenta.getColumnModel().getColumn(0).setMinWidth(0);
        tbnVenta.getColumnModel().getColumn(0).setPreferredWidth(0);
        tbnVenta.getColumnModel().getColumn(0).setMaxWidth(0);

        tbnVenta.getColumnModel().getColumn(5).setCellRenderer(new MiRenderBoton(true));
    }

    public static void cargarModelo(int tienda) {
        ModeloC oC = new ModeloC();
        List<ModeloM> oMs = oC.listarModelo(tienda);
        ArrayList oList;
        limpiarTablaModelo();
        if (oMs != null) {
            int size = oMs.size();
            for (int a = 0; a < size; a++) {
                oList = new ArrayList();
                oList.add(" " + oMs.get(a).getNomModelo());
                oList.add(oMs.get(a).getStockModelo());
                oList.add(oMs.get(a).getPrecio());
                oList.add(oMs.get(a));
                ((ArrayListTableModel) tbnModelo.getModel()).addRow(oList);
            }
        }
    }

    public static void limpiarTablaModelo() {
        ArrayListTableModel oModelTbl = (ArrayListTableModel) tbnModelo.getModel();
        int i = oModelTbl.getRowCount();
        for (int a = 0; a < i; a++) {
            oModelTbl.removeRow(0);
        }
    }

    private void cargarTienda(int tienda, int modelo) {
        TiendaC oC = new TiendaC();
        //  System.out.println(tienda + " - " + modelo);
        List<TiendaM> oMs = oC.listarStockTienda(tienda, modelo);
        ArrayList oList;
        limpiarTablaTienda();
        if (oMs != null) {
            // System.out.println("entro");
            int size = oMs.size();
            for (int a = 0; a < size; a++) {
                oList = new ArrayList();
                oList.add(" " + oMs.get(a).getNomTienda());
                oList.add(oMs.get(a).getStockModelo());
                oList.add(oMs.get(a));
                ((ArrayListTableModel) tbnTiendas.getModel()).addRow(oList);

            }
        }
    }

    private void limpiarTablaTienda() {
        ArrayListTableModel oModelTbl = (ArrayListTableModel) tbnTiendas.getModel();
        int i = oModelTbl.getRowCount();
        for (int a = 0; a < i; a++) {
            oModelTbl.removeRow(0);
        }
    }

    public static void calcularImporte() {
        int filas = tbnVenta.getRowCount();
        @SuppressWarnings("UnusedAssignment")
        double importe = 0, descuento = 0, igv = 0, total = 0;
        for (int i = 0; i < filas; i++) {
            importe += Double.parseDouble(tbnVenta.getValueAt(i, 4).toString());
        }
        txtSubTotal.setText(importe + "");

        if (!"".equals(txtDesc.getText())) {
            descuento = importe * Double.parseDouble(txtDesc.getText()) / 100;
            txtDescuento.setText(descuento + "");
        } else {
            if ("".equals(txtSubTotal.getText())) {
                txtDescuento.setText("");
            } else {
                if (!"".equals(txtDescuento.getText())) {
                    descuento = Double.parseDouble(txtDescuento.getText());
                }

            }
        }
        if (cbnDocumento.getSelectedIndex() != 0) {
            igv = importe * 0.19;
            txtIgv.setText(igv + "");
        } else {
            txtIgv.setText("");
        }

        total = importe - descuento + igv;

        txtTotal.setText(total + "");

        if ("0.0".equals(txtSubTotal.getText())) {
            txtSubTotal.setText("");
        }
        if ("0.0".equals(txtTotal.getText())) {
            txtTotal.setText("");
        }

//        if (!"".equals(txtTotal.getText())) {
//            btnCobrar.requestFocus();
//        }
    }

    public void inicializarCampos(TiendaM oM) {
        txtUsuario.setText(oM.getNomUsuario());
        txtTienda.setText(oM.getNomTienda());
        cbnVendedor.setSelectedItem(oM.getNombres());
        calcularNroVenta oNroVenta = new calcularNroVenta(txtFecha.getText());
        txtNroVenta.setText(oNroVenta.convertirNroventa());
        cargarModelo(xIdTienda);
    }

    private void cargarComboVendedores() {
        UsuarioC oC = new UsuarioC();
        List<UsuarioM> oMs = oC.listarUsuarios();
        String[] oList;
        if (oMs != null) {
            int size = oMs.size();
            oList = new String[size];
            for (int a = 0; a < size; a++) {
                oList[a] = oMs.get(a).getNomCompletos();
            }
            listaUsuario = oList;
        }

    }

    private int otraTienda(int id) {
        if (id == 2) {
            return 3;
        } else {
            if (id == 3) {
                return 2;
            } else {
                return -1;
            }
        }

    }

    private void configurar() {
    }
}